package net.brucecooper.mindmap.client;

import gwt.canvas.client.Image;
import gwt.canvas.client.ImageListener;

import java.util.HashMap;
import java.util.Map;


public class IconManager {

    public static final String ICON_PATH = "http://cactus-wave.appspot.com/icons/";
    private static boolean local;
	private static ImageListener listener;
    
    static Map<String,Image> canvasImages = new HashMap<String,Image>();

    public static void setListener(ImageListener l2) {
    	listener = l2;
    }
    
    public static String getIconURL(String iconName) {
        if (local) {
            return "/icons/" + iconName;
        } else {
        return ICON_PATH + iconName;
        }
    }

    public static String makeImgHTML(String alt, String iconName) {
        return "<img alt=\"" + alt + "\" src=\"" + getIconURL(iconName) + "\">";
    }
    
    public static void setLocal(boolean local) {
        IconManager.local = local;
    }

	public static Image getCanvasImage(String name) {
		Image img = canvasImages.get(name);
		if (img == null) {
			String url = getIconURL(name);
			img = new Image(url, listener);
			canvasImages.put(name, img);
		}
		return img;
	}

}
